<?php

!defined('IN_ROOT') && exit('Access Denied');
//$mid 0 地区
class tagmodel {

    var $db;
    var $base;

    function tagmodel(&$base) {
        $this->base = $base;
        $this->db = $base->db;
    }
    function get($id) {
        return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."tag WHERE id='$id'");
    }
    function remove_by_id($sid,$type=0) {
    	$this->db->query("DELETE FROM `".DB_TABLEPRE."tag` WHERE `id` IN ($id)");
    }
    function deltag($sid,$ids,$type=0) {
    	$this->db->query("DELETE FROM `".DB_TABLEPRE."tag` WHERE `sid`='$sid' and `cid` IN ($ids) and `stype`='$type'");
    }
    function addareatag($name,$aid){
    	$this->db->query('UPDATE `'.DB_TABLEPRE."area` SET ename='$name' WHERE `id`='$aid'");
    }
    function addtag($name,$aid,$mid=0){
    	$oldid = (int)$this->db->result_first("SELECT id FROM `".DB_TABLEPRE."tag` WHERE name='$name'");//先查看是不是存在同名的
    	if($oldid == 0){//如果不存在则添加
    		$this->db->query('INSERT INTO '.DB_TABLEPRE."tag(`name`,`num`) values ('$name','1')");
	        $tagid = $this->db->insert_id();
	        $this->db->query('INSERT INTO '.DB_TABLEPRE."tagmap(`tid`,`aid`,`mid`) values ('$tagid','$aid','$mid')");
	        return $tagid;
    	}else{
    		return 0;
    	}
    }
}
?>
